<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="地热井名称" prop="no">
        <el-input v-model="queryParams.no" placeholder="请输入地热井名称" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="时间">
        <el-date-picker v-model="daterangeTime" size="small" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="流量 >" prop="value">
        <el-input v-model="queryParams.value" placeholder="请输入流量" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['dr:monitoring:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['dr:monitoring:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['dr:monitoring:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <importData :authority="importData_authority" :api="importData_api" :title="importData_title" :templateName="importData_templateName" :isUpdateSupport="false" :point="importData_point"></importData>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['dr:monitoring:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="monitoringList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--  <el-table-column label="序号" align="center" type="index" /> -->
      <el-table-column label="编号" align="center" prop="id" width="55" />
      <el-table-column label="地热井名称" align="center" prop="no">
        <template slot-scope="scope">
          <span>{{$store.getters.wellno2name(scope.row.no)}}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="密码" align="center" prop="psw" /> -->
      <el-table-column label="时间" align="center" prop="time" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.time, '{y}-{m}-{d} {h}:{m}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="流量/m³" align="center" prop="value" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['dr:monitoring:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['dr:monitoring:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改累计流量对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="地热井名称" v-show="title == '添加累计流量'">
          <itemOptions :queryParams="itemParams.item_queryParams" :optionsParam="itemParams.item_optionsParam" @changeItemData="getItemOptionsValue2"></itemOptions>
        </el-form-item>
        <el-form-item label="地热井编号" prop="no">
          <el-input v-model="form.no" placeholder="地热井编号" />
        </el-form-item>
        <el-form-item label="时间" prop="time">
          <el-date-picker clearable size="small" v-model="form.time" type="date" value-format="yyyy-MM-dd" placeholder="选择时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="流量" prop="value">
          <el-input v-model="form.value" placeholder="请输入流量" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listMonitoring,
  getMonitoring,
  delMonitoring,
  addMonitoring,
  updateMonitoring,
} from "@/api/monitoring/total_flow";
import ItemOptions from "@/components/ItemOptions";
import ImportData from "@/components/ImportData";
export default {
  components: { ItemOptions, ImportData },
  name: "Monitoring",
  data() {
    return {
      importData_authority: "dr:common:import",
      importData_api: "/common/",
      importData_title: "导入累计流量数据",
      importData_templateName: "累计流量数据模板",
      importData_point: "累计流量",
      itemParams: {
        item_queryParams: {
          wellname: null,
          welltype: null,
          utilizationmode: null,
          enabled: null,
          entryname: null,
        },
        item_optionsParam: "well",
        itemSelectedValue: "",
      },
      // 遮罩层
      loading: true,
      // 选中数组
      nos: [],
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 累计流量表格数据
      monitoringList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 流量时间范围
      daterangeTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        no: null,
        time: null,
        value: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getItemOptionsValue2(data) {
      this.form.no = data;
      console.log(this.form.no);
    },
    /** 查询累计流量列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeTime && "" != this.daterangeTime) {
        this.queryParams.params["beginTime"] = this.daterangeTime[0];
        this.queryParams.params["endTime"] = this.daterangeTime[1];
      }
      console.log(this.queryParams);
      listMonitoring(this.queryParams).then((response) => {
        this.monitoringList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        no: null,
        psw: null,
        time: null,
        value: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.nos = selection.map((item) => item.no);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加累计流量";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getMonitoring(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改累计流量";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateMonitoring(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMonitoring(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const nos = row.no || this.nos;
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除地热井编号为"' + ids + '"的数据项？')
        .then(function () {
          return delMonitoring(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "dr/monitoring/export",
        {
          ...this.queryParams,
        },
        `monitoring_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
